package org.lex.notepad.ui.preferences;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;

import javax.swing.Box;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;


import org.lex.app.prefs.ResourceMap;
import org.lex.notepad.Notepad;
import org.lex.notepad.util.CK;
import org.lex.notepad.util.laf.InstalledLookAndFeelsList;
import org.lex.swing.prefs.CategoryAdapter;
import org.lex.swing.widgets.FontSelectorButton;
import org.lex.swing.widgets.JLabelSeparator;
import org.lex.utils.Codec;

public class AppearanceCategory extends CategoryAdapter {
	private JPanel fontPane = null;
	private JPanel lookAndFeelsPane = null;
	private JScrollPane lookAndFeelsScrollPane = null;
	private InstalledLookAndFeelsList lookAndFeelsList = null;
	private JPanel classPane = null;
	private JLabel classLabel = null;
	private JTextField classTextField = null;
	private JLabel fontLabel = null;
	private FontSelectorButton fontButton = null;

	@Override
	public String getTitle() {
		return "Notepad.Appearance";
	}

	@Override
	public void loadConfiguration() {
		ResourceMap cfg = Notepad.getApplication().getContext().getResourceManager().getResourceMap();
		// font
		Font font = Codec.decodeFont(cfg.getString(CK.UI_FONT));
		this.getFontButton().setSelectedFont(font);
		// look-and-feel
		String lnfClass = cfg.getString(CK.UI_LOOK_AND_FEEL);
		this.getLookAndFeelsList().setSelectedLookAndFeelClass(lnfClass);
		this.fireContentChanged(false);
	}

	@Override
	public void saveConfiguration() {
		ResourceMap cfg = Notepad.getApplication().getContext().getResourceManager().getResourceMap();
		// font
		cfg.putString(CK.UI_FONT, Codec.encodeFont(this.getFontButton().getSelectedFont()));
		// look-and-feel
		String lnf = getLookAndFeelsList().getSelectedLookAndFeelClassName();
		cfg.putString(CK.UI_LOOK_AND_FEEL, lnf);
	}

	public AppearanceCategory() {
		super();
		this.initialize();
	}

	private void initialize() {
		BorderLayout layout = new BorderLayout();
		layout.setVgap(12);
		this.setLayout(layout);
		this.add(getFontPane(), BorderLayout.NORTH);
		this.add(getLookAndFeelsPane(), BorderLayout.CENTER);
	}

	private JPanel getFontPane() {
		if (fontPane == null) {
			FlowLayout flowLayout = new FlowLayout();
			flowLayout.setAlignment(FlowLayout.LEFT);
			flowLayout.setHgap(0);
			fontLabel = new JLabel();
			fontLabel.setText("Font");
			fontLabel.setBorder(new EmptyBorder(0, 0, 0, 11));
			fontPane = new JPanel();
			fontPane.setLayout(flowLayout);
			fontPane.add(fontLabel, null);
			fontPane.add(Box.createHorizontalStrut(5));
			fontPane.add(getFontButton(), null);
		}
		return fontPane;
	}

	private JPanel getLookAndFeelsPane() {
		if (lookAndFeelsPane == null) {
			BorderLayout layout = new BorderLayout();
			layout.setVgap(6);
			lookAndFeelsPane = new JPanel(layout);
			lookAndFeelsPane.add(new JLabelSeparator("Look And Feel"), BorderLayout.NORTH);
			lookAndFeelsPane.add(getLookAndFeelsScrollPane(), BorderLayout.CENTER);
			lookAndFeelsPane.add(getClassPane(), BorderLayout.SOUTH);
		}
		return lookAndFeelsPane;
	}

	private JScrollPane getLookAndFeelsScrollPane() {
		if (lookAndFeelsScrollPane == null) {
			lookAndFeelsScrollPane = new JScrollPane();
			lookAndFeelsScrollPane.setViewportView(getLookAndFeelsList());
		}
		return lookAndFeelsScrollPane;
	}

	private InstalledLookAndFeelsList getLookAndFeelsList() {
		if (lookAndFeelsList == null) {
			lookAndFeelsList = new InstalledLookAndFeelsList();
			lookAndFeelsList.addListSelectionListener(new ListSelectionListener() {
				public void valueChanged(ListSelectionEvent e) {
					if (true == e.getValueIsAdjusting())
						return;
					int selected = getLookAndFeelsList().getSelectedIndex();
					if (-1 == selected)
						return;
					fireContentChanged(true);
					getClassTextField().setText(lookAndFeelsList.getSelectedLookAndFeelClassName());
				}
			});
		}
		return lookAndFeelsList;
	}

	private JPanel getClassPane() {
		if (classPane == null) {
			classLabel = new JLabel();
			classLabel.setText("Class");
			BorderLayout borderLayout = new BorderLayout();
			borderLayout.setHgap(5);
			classPane = new JPanel();
			classPane.setLayout(borderLayout);
			classPane.add(classLabel, java.awt.BorderLayout.WEST);
			classPane.add(getClassTextField(), java.awt.BorderLayout.CENTER);
		}
		return classPane;
	}

	private JTextField getClassTextField() {
		if (classTextField == null) {
			classTextField = new JTextField();
			classTextField.setEditable(false);
		}
		return classTextField;
	}

	private FontSelectorButton getFontButton() {
		if (fontButton == null) {
			fontButton = new FontSelectorButton("Font");
			fontButton.addFontChangeListener(this);
		}
		return fontButton;
	}
}
